package com.mylibraryapplication.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mylibraryapplication.R;
import com.mylibraryapplication.base.BaseActivity;
import com.mylibraryapplication.view.TitleView;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * @author yinsujun  Created at 2016/9/7 11:09
 */
public class RxJavaActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.rx_bt)
    Button rxBt;
    @Bind(R.id.rx_tv_result)
    TextView rxTvResult;

    @Override
    public int getLayoutId() {
        return R.layout.activity_rx_test;
    }

    @Override
    public void initView() {
        setTitle(title,"RxJava");
        testRx();
    }

    private void testRx() {

        rxBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBtClick();
            }
        });
    }


    public static final int DELAY_SECONDS = 60;
    private void onBtClick(){
//        Observable.just("test1","test2","test3","test2","test3","test2","test3","test2","test3","test3").subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
//                Log.e(TAG,s);
//
//            }
//        });
//        }

        startTimer();


//        query().flatMap(new Func1<List<String>, Observable<?>>() {
//            @Override
//            public Observable<?> call(List<String> strings) {
//                return Observable.from(strings);
//            }
//        }).filter(new Func1<Object, Boolean>() {
//            @Override
//            public Boolean call(Object s) {
//                return s.toString().contains("a");
//            }
//        }).take(3).doOnNext(new Action1<Object>() {
//            @Override
//            public void call(Object o) {
//                Log.e(TAG,"test=="+o.toString());
//            }
//        }).subscribe(new Action1<Object>() {
//            @Override
//            public void call(Object o) {
//                Log.e(TAG,o.toString());
////                rxTvResult.setText(o.toString());
//            }
//        });
    }
//    static Observable<List<String>>query(){
//        List<String> s = Arrays.asList("Java", "Android", "Ruby", "Ios", "Swift");
//        return Observable.just(s);
//    }

    private int timer=60;
    private  Subscription sub;
    private void startTimer() {
        sub=Observable.timer(1,TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .repeat(60)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        timer--;
                        if (timer > 0) {
                            rxBt.setText("重新发送" + timer + "s");
                        } else {
                            timer = DELAY_SECONDS;
                            rxBt.setText("重新发送");
                            rxBt.setEnabled(true);
                        }
                        Log.e(TAG,"倒计时=="+timer);
                    }
                });
//        s.isUnsubscribed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(sub!=null&&sub.isUnsubscribed()){
            sub.unsubscribe();
        }
    }
}
